package ltd.sshine.bot.config;


import ltd.sshine.bot.DTO.User;
import ltd.sshine.bot.config.Util.EventUtil;
import ltd.sshine.bot.stroge.UserUtil;
import net.mamoe.mirai.Bot;
import net.mamoe.mirai.BotFactory;
import net.mamoe.mirai.contact.Contact;
import net.mamoe.mirai.event.Event;
import net.mamoe.mirai.event.EventChannel;
import net.mamoe.mirai.event.GlobalEventChannel;
import net.mamoe.mirai.event.events.BotEvent;
import net.mamoe.mirai.event.events.MessageEvent;
import net.mamoe.mirai.message.data.At;
import net.mamoe.mirai.message.data.MessageChain;
import net.mamoe.mirai.message.data.SingleMessage;
import net.mamoe.mirai.utils.BotConfiguration;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestTemplate;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class BotConfig  {

    @Value("${qq:1438423742}")
    public  Long qq;
    @Value("${password:13978960539}")
    public String password;

    @Bean
    public Bot getBot(){
        BotConfiguration botConfiguration = new BotConfiguration();
        ClassPathResource classPathResource = new ClassPathResource("device.json");
        botConfiguration.fileBasedDeviceInfo(classPathResource.getPath());
        botConfiguration.setProtocol(BotConfiguration.MiraiProtocol.ANDROID_PHONE);
        Bot bot = BotFactory.INSTANCE.newBot(qq,password,botConfiguration);
        bot.login();
        EventChannel<Event> channel = GlobalEventChannel.INSTANCE.filter(ev -> ev instanceof BotEvent);
        channel.subscribeAlways(MessageEvent.class, event -> {
            MessageChain message = event.getMessage();
            SingleMessage singleMessage = message.get(1);
            String[] e=singleMessage.contentToString().split("\\s");
            EventUtil.run(e[0],event);
        });
        return bot;
    }
}
